﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RED_NET_Core.DAL;
using RED_NET_Core.Entity;
using System.Data;
using System.Data.SqlClient;
using RED_NET_Core.Utilty;
namespace RED_NET_Core.BLL
{
    public class News_BLL
    {
        #region[添加普通日志]
        /// <summary>
        /// 添加普通日志
        /// </summary>
        /// <param name="nUId">用户Id</param>
        /// <param name="nContent">日志内容</param>
        /// <param name="nTitle">日志标题</param>
        public static int AddDaily(int nUId, string nContent, string nTitle)
        {
            Login_DAL _login = new Login_DAL();
            Users _users = _login.GetMessageOfUser(nUId);
            News _news = new News();
            _news.UId = nUId;
            _news.Level = 2;
            _news.ReplyNum = 0;
            _news.Content = nContent;
            _news.Title = nTitle;
            _news.UserName = _users.UserName;
            _news.AddTime = System.DateTime.Now;
            _news.UpdateTime = System.DateTime.Now;
            _news.IsDeleted = false;
            _news.SendTo = "null";
            _news.ReadNum = 0;
            string shortContent = Safety.getShortStr(nContent, 45);
            int _ret = -1;
            try
            {
                _ret = News_DAL.AddNews(_news);
                NewsFeed_DAL newsfeed = new NewsFeed_DAL();
               newsfeed.InsertNewsFeed(nUId, 1, nTitle,shortContent, _news.UpdateTime, _ret, 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _ret;

        }
        #endregion
        #region[添加英雄帖]
        /// <summary>
        /// 添加英雄帖
        /// </summary>
        /// <param name="nUId"></param>
        /// <param name="nCollegeId"></param>
        /// <param name="nContent"></param>
        /// <param name="nTitle"></param>
        /// <returns></returns>
        public static int AddHeroPoint(int nUId,int nCollegeId, string nContent, string nTitle)
        {
            int _ret;
            try
            {
            
            int[] SendTo = new int[500];
            int num;
            LoginBLL _loginbll = new LoginBLL();
            DataTable _dt = _loginbll.GetClassUser(nCollegeId);
            num = _dt.Rows.Count;
            int i;
            for (i = 0; i < num; i++)
            {
                SendTo[i] = Int32.Parse(_dt.Rows[i]["_UId"].ToString());
            }
            Login_DAL _login = new Login_DAL();
            Users _users = _login.GetMessageOfUser(nUId);
            News _news = new News();
            _news.UId = nUId;
            _news.Level = 1;
            _news.ReplyNum = 0;
            _news.Content = nContent;
            _news.Title = nTitle;
            _news.UserName = _users.UserName;
            _news.AddTime = System.DateTime.Now;
            _news.UpdateTime = System.DateTime.Now;
            _news.IsDeleted = false;
            _news.SendTo = "全院用户";
            _news.ReadNum = 0;
               
            Letter _letter = new Letter();
            _letter.IsRead = 0;
            _letter.UIdOfSend = _news.UId;
     
        
                _ret=_letter.LetterId = _news.AddItem();
                   NewsFeed_DAL newsfeed = new NewsFeed_DAL();
                   newsfeed.InsertNewsFeed(nUId, 2, nTitle, nContent, _news.UpdateTime, _ret, 0);
                for (i = 0; i < num; i++)
                {
                    _letter.UIdOfRecv = SendTo[i];
                    _letter.AddItem();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _ret;
        }
        #endregion
        #region[日志访问人数加一]
        /// <summary>
        /// 日志访问人数加一
        /// </summary>
        /// <param name="_NewsId">日志Id</param>
        public static void AddDailyReadNum(int _NewsId)
        {
           
            try
            {
                News_DAL.AddDailyReadNum(_NewsId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[获取日志访问人数]
        /// <summary>
        /// 获取日志访问人数
        /// </summary>
        /// <param name="_NewsId">日志Id</param>
        /// <returns></returns>
        public static int GetDailyReadNum(int _NewsId)
        {
            int _ans = -1;
            _ans = News_DAL.GetDailyReadNum(_NewsId);
            return _ans;

        }
        #endregion
        #region[判断站内信是否为该用户所有]
        /// <summary>
        /// 判断站内信是否为该用户所有
        /// </summary>
        /// <param name="id"></param>
        /// <param name="news"></param>
        /// <returns></returns>
        public static int JudgeRecvLetter(int uid, int letterId)
        {
            try{
             News_DAL.JudgeRecvLetter(uid, letterId);
            }
            catch (Exception ex)
            {
                return 0;

            }
            return 1;
        }
        #endregion
        #region[添加留言]
        public static void AddMessage(int nUId, string nContent, int nMessageTo)
        {
            Login_DAL _login = new Login_DAL();
            Users _users = _login.GetMessageOfUser(nUId);
            News _news = new News();
            _news.UId = nUId;
            _news.Level = 4;
            _news.ReplyNum = 0;
            _news.Content = nContent;
            _news.Title = "null";
            _news.UserName = _users.UserName;
            _news.AddTime = System.DateTime.Now;
            _news.UpdateTime = System.DateTime.Now;
            _news.IsDeleted = false;
            _news.SendTo = "null";
            _news.MessageTo = nMessageTo;
            try
            {
                News_DAL.AddMessage(_news);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[添加站内信]
        /// <summary>
        /// 添加站内信
        /// </summary>
        /// <param name="nUId">用户Id</param>
        /// <param name="nContent">站内信内容</param>
        /// <param name="nTitle">站内信标题</param>
        /// <param name="nSendTo">站内信接受者</param>
        public static void AddLetter(int nUId, string nContent, string nTitle, int[] nSendTo, int nNum, string nSendToStr)
        {
            Login_DAL _login = new Login_DAL();
            Users _users = _login.GetMessageOfUser(nUId);
            News _news = new News();

            _news.UId = nUId;
            _news.Level = 3;
            _news.ReplyNum = 0;
            _news.Content = nContent;
            _news.Title = nTitle;
            _news.UserName = _users.UserName;
            _news.AddTime = System.DateTime.Now;
            _news.UpdateTime = System.DateTime.Now;
            _news.Time = 0;
            _news.SendTo = nSendToStr;
            _news.IsDeleted = false;
            try
            {
                News_DAL.AddLetter(_news, nSendTo, nNum);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[删除站内信]
        /// <summary>
        /// 删除站内信
        /// </summary>
        /// <param name="nUId"></param>
        public static void DeleteLetter(int nUId)
        {
            try
            {
                News_DAL.DeleteLetter(nUId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[获得收到的站内信列表]
        /// <summary>
        /// 获得收到的站内信
        /// </summary>
        /// <param name="nUId"></param>
        /// <returns></returns>
        public static DataTable ShowRecvLetterList(int nUId)
        {
            DataTable _dt = new DataTable();
            try
            {
                _dt = News_DAL.ShowRecvLetterList(nUId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _dt;
        }
        #endregion
        #region[获取留言列表]
        /// <summary>
        /// 获取留言列表
        /// </summary>
        /// <param name="nUId"></param>
        /// <returns></returns>
        public static DataTable ShowMessageList(int nUId)
        {
            DataTable _dt = new DataTable();
            try
            {
                _dt = News_DAL.ShowMessageList(nUId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _dt;

        }
        #endregion
        #region[删除留言]
        public static void DeleteMessage(int nId)
        {
            try
            {
                News_DAL.DeleteMessage(nId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region[获得发出的站内信列表]
        /// <summary>
        /// 获得发出的站内信
        /// </summary>
        /// <param name="nUId"></param>
        /// <returns></returns>
        public static DataTable ShowSendLetterList(int nUId)
        {
            try
            {
                return News_DAL.ShowSendLetterList(nUId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[获取新闻内容]
        public static News ShowNews(int nId)
        {
            News _news = new News();
            try
            {
                _news = News_DAL.ShowNews(nId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _news;
        }
        #endregion
        #region[添加英雄帖]
        /// <summary>
        /// 添加英雄帖
        /// </summary>
        /// <param name="nUId">用户Id</param>
        /// <param name="nContent">英雄帖内容</param>
        /// <param name="nTitle">英雄帖标题</param>
        public static void AddHerrorPosts(int nUId, string nContent, string nTitle, int nTime)
        {
            try
            {
              
                Login_DAL _login = new Login_DAL();
                Users _users = _login.GetMessageOfUser(nUId);
                News _news = new News();
                _news.UId = nUId;
                _news.Level = 1;
                _news.ReplyNum = 0;
                _news.Content = nContent;
                _news.Title = nTitle;
                _news.UserName = _users.UserName;
                _news.Time = nTime;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // News_DAL.AddNews(_news);
        }
        #endregion
        #region[添加公告]
        /// <summary>
        /// 添加公告
        /// </summary>
        /// <param name="nUId">用户Id</param>
        /// <param name="nContent">公告内容</param>
        /// <param name="nTitle">公告标题</param>
        public static void AddAnnouncement(int nUId, string nContent)
        {
            try
            {
                Login_DAL _login = new Login_DAL();
                Users _users = _login.GetMessageOfUser(nUId);
                News _news = new News();
                _news.UId = nUId;
                _news.Level = 0;
                _news.ReplyNum = 0;
                _news.Content = nContent;
                _news.Title = " ";
                _news.UserName = _users.UserName;
                _news.IsDeleted = false;
                _news.MessageTo = 0;
                _news.ReadNum = 0;
                _news.SendTo = " ";
                _news.Time = 0;
                _news.UpdateTime = DateTime.Now;
                _news.AddTime = DateTime.Now;
                _news.AddItem();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[获取公告]
        public static DataTable GetAnnouncement(int num)
        {
            try
            {
                return News_DAL.GetAnnouncement(num);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[删除公告]
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="_Id"></param>
        public static void DeleteAnnouncement(int nId)
        {
            try
            {
                News_DAL.DeleteAnnouncement(nId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[添加评论]
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="nUId">评论者ID</param>
        /// <param name="nIdTo">被评论者的ID</param>
        /// <param name="nContent">评论内容</param>
        public static void AddComment(int nUId, int nIdTo, string nContent)
        {


            Login_DAL _login = new Login_DAL();
            Users _users = _login.GetMessageOfUser(nUId);
            Comment _comment = new Comment();
            _comment.UId = nUId;
            _comment.IdTo = nIdTo;
            _comment.Level = false;
            _comment.Tent = nContent;
            _comment.AddTime = System.DateTime.Now;
            _comment.UserName = _users.NickName;
            try
            {
                News_DAL.AddComment(_comment);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[删除新闻]
        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="nId">新闻Id</param>
        public static void DeleteNews(int nId)
        {
            try
            {

                News_DAL.DeleteNews(nId);
                NewsFeed_DAL newsfeed = new NewsFeed_DAL();
                newsfeed.DeleteNewsFeed(1, nId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[删除评论]
        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="nId">评论的Id</param>
        public static void DeleteComment(int nId)
        {
            try
            {
                News_DAL.DeleteComment(nId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region[获取未读的站内信总数]
        public static int GetLetterNum(int nUId)
        {
            int _ret = -1;
            try
            {
                _ret = News_DAL.GetLetterNum(nUId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _ret;

        }
        #endregion
        #region[获取日志列表]
        /// <summary>
        /// 获取日志
        /// </summary>
        /// <param name="nUid">用户id</param>
        /// <param name="nStat">stat=1:全部日志，stat=2:关注人
        public static DataTable ShowDailyList(int nUId, int nStat)
        {
            DataTable _dt = new DataTable();
            try
            {
                _dt = News_DAL.ShowDailyList(nUId, nStat);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _dt;

        }
        #endregion
        #region[ 获取评论列表]
        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="nId">被评论的新闻id</param>
        /// <returns>从数据库获取的评论</returns>
        public static DataTable ShowCommentList(int nId)
        {
            DataTable _dt = new DataTable();
            try
            {
                _dt = News_DAL.ShowCommentList(nId);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _dt;
        }
        #endregion
        #region[设置站内信为已读]
        /// <summary>
        /// 设置站内信为已读
        /// </summary>
        /// <param name="nId">设置的letter id</param>
        public static void SetLetterIsRead(int nId)
        {
            try
            {
                News_DAL.SetLetterIsRead(nId);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion
        #region[更新日志]
        /// <summary>
        /// 更新日志
        /// </summary>
        /// <param name="nContent">内容</param>
        /// <param name="nTitle">标题</param>
        /// <param name="nId">日志ID</param>
        public static void UpdateDaily(string nContent, string nTitle, int nId,int uId)
        {
            string shortContent = Safety.getShortStr(nContent, 45);
            try
            {
                News_DAL.UpdateDaily(nContent, nTitle, nId);
                NewsFeed_DAL newsfeed = new NewsFeed_DAL();
                newsfeed.InsertNewsFeed(uId,1,nTitle,shortContent,System.DateTime.Now,nId,0);

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion

    }
}
